import java.util.ArrayList;
import java.util.List;

public class UserService implements Loggable {
    private List<User> userList = new ArrayList<>();

    // 构造方法中初始化默认管理员
    public UserService() {
        initDefaultAdmin();
    }

    private void initDefaultAdmin() {
        // 默认管理员，确保中文正常显示
        userList.add(new Admin("A001", "admin", "ynuinfo#777", "13800138000"));
        System.out.println("默认管理员初始化完成：用户名 admin，密码 ynuinfo#777");
    }

    public User login(String username, String password) {
        for (User user : userList) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                logOperation("用户登录：" + username);
                return user;
            }
        }
        return null;
    }

    public boolean addUser(User user) {
        // 检查ID和用户名唯一性
        if (userList.stream().anyMatch(u -> u.getUserId().equals(user.getUserId()) ||
                                        u.getUsername().equals(user.getUsername()))) {
            return false;
        }
        userList.add(user);
        logOperation("添加用户：" + user.getUsername());
        return true;
    }

    public List<User> getAllUsers() {
        return userList;
    }
}